﻿using CrawlerCore.Helper;
using CrawlerCore.XmlDataForeign;
using CrawlerCore.XmlDataTemp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace CrawlerCore.XmlData
{
    public class Root
    {
        [XmlElement("OutBound")]
        public OutInBound outBound { get; set; }
        [XmlElement("InBound")]
        public OutInBound inBound { get; set; }
        public string Departure { get; set; }

        public string Arrival { get; set; }

        public string AirLine = "";
        public RootForeign Convert2RootForeign(SearchInputService input)
        {
            RootForeign r = new RootForeign();
            try
            {
                r.Departure = Departure;
                r.Arrival = Arrival;
                r.Flights = getListFlights(input);
            }
            catch { }
            return r;
        }

        private List<FlightForeign> getListFlights(SearchInputService input)
        {
            List<FlightForeign> li = new List<FlightForeign>();
            bool isRoundTrip = true;
            if (inBound == null || inBound.Items == null || inBound.Items.Count == 0)
                isRoundTrip = false;
            try
            {
                int id = 0;
                if (isRoundTrip)
                {
                    foreach (Item it in outBound.Items)
                    {
                        if (it.price.Count == 0)
                            continue;
                        foreach (Item it1 in inBound.Items)
                        {
                            if (it1.price.Count == 0)
                                continue;
                            FlightForeign f = new FlightForeign();
                            f.Id = id;
                            id++;
                            f.Source = AirLineName.vietJetAir;
                            //chieu di
                            OutInBoundForeign outBoundForeign = new OutInBoundForeign();
                            outBoundForeign.Segments = Convert2ListSegmentForeign(it, true);

                            f.outBoundForeign = outBoundForeign;
                            //chieu ve
                            OutInBoundForeign inBoundForeign = new OutInBoundForeign();
                            inBoundForeign.Segments = Convert2ListSegmentForeign(it1, false);
                            f.inBoundForeign = inBoundForeign;
                            //tam
                            Price pOut = it.price.OrderBy(x => decimal.Parse(x.price)).FirstOrDefault();
                            Price pIn = it1.price.OrderBy(x => decimal.Parse(x.price)).FirstOrDefault();
                            f.PriceBaseOut = decimal.Parse(pOut.price);
                            f.PriceTaxOut = decimal.Parse(pOut.taxPrice);
                            f.PriceBaseIn = decimal.Parse(pIn.price);
                            f.PriceTaxIn = decimal.Parse(pIn.taxPrice);
                            li.Add(f);
                        }
                    }
                }
                else
                {
                    foreach (Item it in outBound.Items)
                    {
                        if (it.price.Count == 0)
                            continue;
                        FlightForeign f = new FlightForeign();
                        f.Id = id;
                        id++;
                        f.Source = AirLineName.vietJetAir;
                        //chieu di
                        OutInBoundForeign outBoundForeign = new OutInBoundForeign();
                        outBoundForeign.Segments = Convert2ListSegmentForeign(it, true);
                        f.outBoundForeign = outBoundForeign;
                        //tam
                        Price pOut = it.price.OrderBy(x => decimal.Parse(x.price)).FirstOrDefault();
                        f.PriceBaseOut = decimal.Parse(pOut.price);
                        f.PriceTaxOut = decimal.Parse(pOut.taxPrice);
                        li.Add(f);
                    }
                }
            }
            catch { }
            return li;
        }

        private List<SegmentForeign> Convert2ListSegmentForeign(Item it, bool isOutBound)
        {
            List<SegmentForeign> li = new List<SegmentForeign>();
            SegmentForeign s = new SegmentForeign();
            s.AirLine = it.flightNo.Substring(0, 2);
            s.Departure = isOutBound ? Departure : Arrival;
            s.Arrive = isOutBound ? Arrival : Departure;
            s.FlightNo = it.flightNo;
            s.DepartureTime = DateTime.Parse(it.departureTime);
            s.ArriveTime = DateTime.Parse(it.arriveTime);
            s.LayOver = "";
            s.Class = it.price.OrderBy(x => decimal.Parse(x.price)).FirstOrDefault().name;
            TimeSpan duration = s.ArriveTime - s.DepartureTime;
            s.Duration = duration.Hours + "h " + duration.Minutes + "m";
            li.Add(s);
            return li;
        }
    }
}